package com.liankuan.console.service.ServiceImpl;

import com.liankuan.console.dto.UserExchange;
import com.liankuan.console.repository.UserExchangeRepository;
import com.liankuan.console.service.UserExchangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserExchangeServiceImpl implements UserExchangeService {

    private final UserExchangeRepository userExchangeRepository;

    @Autowired
    public UserExchangeServiceImpl(UserExchangeRepository userExchangeRepository) {
        this.userExchangeRepository = userExchangeRepository;
    }

    /**
     * 获取用户对应的所有的交易所IDs
     *
     * @param userId
     * @return
     */
    @Override
    public List<Integer> findAllExchanges(Integer userId) {
        return userExchangeRepository.findAllByUserId(userId).stream().map(UserExchange::getExchangeId).collect
                (Collectors.toList());
    }

    /**
     * 根据用户id和交易所id进行删除操作
     *
     * @param userId
     * @param exchangeId
     */
    public void deleteByUserIdAndExchangeId(int userId, int exchangeId) {
        userExchangeRepository.deleteByUserIdAndExchangeId(userId, exchangeId);
    }

    /**
     * 新增记录
     *
     * @param userExchange
     */
    @Override
    public void save(UserExchange userExchange) {
        userExchangeRepository.save(userExchange);
    }


    /**
     * 当某个交易所删除时直接删除所有的映射关系
     *
     * @param exchangeId
     */
    @Override
    public void deleteByExchangeId(int exchangeId) {
        userExchangeRepository.deleteAllByExchangeId(exchangeId);
    }

    /**
     * 当某个用户所删除时直接删除所有的映射关系
     *
     * @param userId
     */
    @Override
    public void deleteByUserId(int userId) {
        userExchangeRepository.deleteAllByUserId(userId);
    }
}
